There are two paths through this chart.
The windshield wiper decision is a two-way decision (sometimes called a binary decision.) It seems small, but in programming complicated decisions are made of many small decisions. Here is a program that includes a binary decision.
import java.util.Scanner;
class NumberTester
{
public static void main (String[] args)
{
Scanner scan = new Scanner( System.in );
int num;
System.out.println("Enter an integer:");
num = scan.nextInt();
if ( num < 0 ) // is num less than zero?
System.out.println("The number " + num + " is negative"); // true branch
else
System.out.println("The number " + num + " is positive"); // false branch
System.out.println("Good-bye for now"); // always executed
}
}
The words if
and else
are markers that divide the
decision into two sections.
The else
divides the true branch from the
false branch.
The if
is followed by a question enclosed in parentheses.
The expression num < 0
num
is less than zero.
if
statment always asks a question (often about a variable.)
Notice that a two-way decision is like picking which of two roads to take
to the same destination.
The fork in the road is the if
statement, and the two roads come together
just after the false branch.
The user runs the program and enters "12". What will the program print?